package com.weijing.pretty_app.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.weijing.pretty_app.R
import com.weijing.pretty_app.databinding.ItemUserBinding
import com.weijing.pretty_app.fragment.UserModel

class UserAdapter(private val list: List<UserModel>) :
    RecyclerView.Adapter<UserAdapter.UserHolder>() {
    private var mPosition: Int = 0

    class UserHolder(val binding: ItemUserBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserHolder {
        return UserHolder(
            ItemUserBinding.inflate(
                LayoutInflater.from(parent.context), parent, false
            )
        )
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: UserHolder, position: Int) {
        val model = list[position]
        with(holder) {
            binding.itemUserTvId.text = "User Id：" + model.id
            binding.itemUserTvName.text = "Name：" + model.name
            binding.itemUserTvSex.text = "Gender：" + model.gender
            binding.itemUserTvPhone.text = "phone：" + model.phone
//            binding.itemUserTvAddress.text = "Address：" + model.address
            binding.itemUserTvIntensity.text =
                binding.root.context.getString(R.string.intensity) + "：${if (model.intensityValue == null) "0" else model.intensityValue}"
            binding.itemUserTvDuration.text =
                binding.root.context.getString(R.string.duration) + "：${if (model.durationValue == null) "0" else model.durationValue}"
            binding.itemUserTvFrequency.text =
                binding.root.context.getString(R.string.Frequency) + "：${if (model.FrequencyValue == null) "0" else model.FrequencyValue}"

            binding.itemUserTvTimes.text =
                "Treatment Times：${if (model.treatmentTimes == null) "0" else model.treatmentTimes}"
            binding.itemUserTvDate.text =
                "Treatment Date：" + if (model.treatmentDate?.isNullOrEmpty() == true) "0" else model.treatmentDate

            if (onClickListener != null) {
                holder.itemView.setOnClickListener { onClickListener?.onItemClickListener(model) }
                binding.handleFragmentClWindowEdit.setOnClickListener {
                    onClickListener?.onEditClick(model)
                }

                binding.handleFragmentClWindowDelete.setOnClickListener {
                    onClickListener?.onDeleteClick(model)
                }
            }
        }
    }

    var onClickListener: OnClickListener? = null

    interface OnClickListener {
        fun onItemClickListener(model: UserModel)

        fun onEditClick(model: UserModel)

        fun onDeleteClick(model: UserModel)
    }
}